package com.companyname.common.util.endecode;

import org.apache.log4j.Logger;

public class UniqueKey
{

        static Logger logger = Logger.getLogger(UniqueKey.class);
        
        /**
         * encodes username and password to a unique secure key
         * 
         * @param String
         *                clear text username
         * @param String
         *                cler text password
         * @return String generated session key
         */
        public static String encode(String username, String password)
                                                                     throws Exception
        {
                logger.debug("Username: " + username + " , Password: " + password);
                int strSize = username.length();
                int strSizeLength = (new Integer(strSize)).toString().length();
                String userKey = "" + strSizeLength + strSize + username;
                strSize = password.length();
                strSizeLength = (new Integer(strSize)).toString().length();
                userKey = userKey + strSizeLength + strSize + password;

                logger.debug("Key: " + userKey);
                String encKey = new String(Base64.encode(userKey.getBytes()),
                                           "UTF-8");
                encKey = new String(Base64.encode(encKey.getBytes()), "UTF-8");
                logger.debug("Encoded Key: " + encKey);
                return encKey;
        }

        /**
         * encodes password to a unique secure key
         * 
         * @param String
         *                cler text password
         * @return String generated session key
         */
        public static String encodePassword(String password) throws Exception
        {

                logger.debug("Password: " + password);
                int strSize = password.length();
                int strSizeLength = (new Integer(strSize)).toString().length();
                String userKey = "" + strSizeLength + strSize + password;
                String encKey = new String(Base64.encode(userKey.getBytes()),
                                           "UTF-8");
                encKey = new String(Base64.encode(encKey.getBytes()), "UTF-8");
                logger.debug("Encrypt Password: " + encKey);
                return encKey;
        }

        /**
         * decodes the unique key to password
         * 
         * @param String
         *                unique session key
         * @return String clear text password
         */
        public static String decodePassword(String encKey) throws Exception
        {

                logger.debug("Encrypt Password: " + encKey);

                String userKey = new String(Base64.decode(encKey
                        .getBytes("UTF-8"), new IntRef()), "UTF-8");
                userKey = new String(Base64.decode(userKey.getBytes("UTF-8"),
                                                   new IntRef()), "UTF-8");

                int strSizeLength = Integer.parseInt(userKey.substring(0, 1));
                int strSize = Integer.parseInt(userKey
                        .substring(1, strSizeLength + 1));
                String password = userKey.substring(strSizeLength + 1, strSize
                        + strSizeLength
                        + 1);
                logger.debug("Password: " + password);
                return password;
        }

        /**
         * decodes the unique key to username and password
         * 
         * @param String
         *                unique session key
         * @return String clear text username
         */
        public static String decodeUname(String encKey) throws Exception
        {

                logger.debug("Encoded Key: " + encKey);

                String userKey = new String(Base64.decode(encKey
                        .getBytes("UTF-8"), new IntRef()), "UTF-8");
                userKey = new String(Base64.decode(userKey.getBytes("UTF-8"),
                                                   new IntRef()), "UTF-8");

                logger.debug("Key: " + userKey);
                int strSizeLength = Integer.parseInt(userKey.substring(0, 1));
                int strSize = Integer.parseInt(userKey
                        .substring(1, strSizeLength + 1));
                String username = userKey.substring(strSizeLength + 1, strSize
                        + strSizeLength
                        + 1);
                logger.debug("Username: " + username);
                return username;
        }

        /**
         * decodes the unique key to username and password
         * 
         * @param String
         *                unique session key
         * @return String clear text password
         */
        public static String decodePwd(String encKey) throws Exception
        {

                logger.debug("Encoded Key: " + encKey);

                String userKey = new String(Base64.decode(encKey
                        .getBytes("UTF-8"), new IntRef()), "UTF-8");
                userKey = new String(Base64.decode(userKey.getBytes("UTF-8"),
                                                   new IntRef()), "UTF-8");

                logger.debug("Key: " + userKey);
                int strSizeLength = Integer.parseInt(userKey.substring(0, 1));
                int strSize = Integer.parseInt(userKey
                        .substring(1, strSizeLength + 1));

                String tempPassword = userKey.substring(strSize
                        + strSizeLength
                        + 1);
                strSizeLength = Integer.parseInt(tempPassword.substring(0, 1));
                strSize = Integer.parseInt(tempPassword
                        .substring(1, strSizeLength + 1));
                String password = tempPassword
                        .substring(strSizeLength + 1, strSize
                                + strSizeLength
                                + 1);

                logger.debug("Username: " + password);
                return password;
        }

        public static void main(String[] args) throws Exception
        {
                String username = "agile";
                String password = "sss";
                String userKey = UniqueKey.encode(username, password);
                System.out.println("userKey=" + userKey);
                String u = null, p = null;
                u = UniqueKey.decodeUname(userKey);
                p = UniqueKey.decodePwd(userKey);
                System.out.println("Username: " + u + " , Password: " + p);

                String passwd = UniqueKey.encodePassword(password);
                System.out.println("passwd=" + passwd);
                p = null;
                p = UniqueKey.decodePassword(passwd);
                System.out.println("Password: " + p);

        }

}
